﻿package com.snzflash.control.scroll 
{
	import com.snzflash.control.scroll.ScrollBase;
	import com.snzflash.events.NavigationEvent;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	/**
	 * Скролл для громкости
	 * 
	 * Базовый класс. используется только в символом в библиотеке
	 * @author firsoff maxim, tel: +79222345364, skype: maxim.firsoff
	 */
	public class VolumeScrollBase extends Sprite
	{
		
		// PROPERTIES
	
		// public
		public var unmuteButton:Sprite;
		public var muteButton:Sprite;
		public var icon:Sprite;
		public var mark:Sprite;
		public var background:Sprite;
		private var _pressed:Boolean;
		private var _position:Number = 1;
		private var _savedPosition:Number = 1;
		
		private var _mute:Boolean = false;
		// private
		
		// == STATIC==
		
		
		// CONSTRUCTOR
		public function VolumeScrollBase() 
		{
			super();
			init();
		};
		
		
		
		// METHODS
	
		// public
		public function destroy():void 
		{
			icon.removeEventListener(MouseEvent.MOUSE_DOWN, onIconMouseDown);
			background.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			stage.removeEventListener(Event.MOUSE_LEAVE, onMouseLeaveFromStage);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove);
			stage.removeEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
			icon.stopDrag();
		};
		
		// private
		private function init():void
		{
			icon.addEventListener(MouseEvent.MOUSE_DOWN, onIconMouseDown);
			background.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			mark.mouseEnabled = false;
			
			unmuteButton.addEventListener(MouseEvent.CLICK, onUnmuteClick);
			muteButton.addEventListener(MouseEvent.CLICK, onMuteClick);
			
			position = _position;
			mute = _mute;
			
		};
		
		
		private function dispatchChange():void
		{
			//var event:NavigationEvent = new NavigationEvent(NavigationEvent.VOLUME, false, false, _position);
			//dispatchEvent(event);
			dispatchEvent(new Event(Event.CHANGE));
		};
		
		// getters/setters
		public function get position():Number { return _position; };
		
		public function set position(value:Number):void 
		{
			_position = value;
			icon.x = _position * background.width;
			mark.width = icon.x;
		};
		
		public function get mute():Boolean { return _mute; };
		
		public function set mute(value:Boolean):void 
		{
			//if (_mute == value) return;
			_mute = value;
			muteButton.visible = !value;
			unmuteButton.visible = value;
		};
		
		// == STATIC==
		
		
		// HANDLERS
		
		private function onAddedToStage(event:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			stage.addEventListener(Event.MOUSE_LEAVE, onMouseLeaveFromStage);
		};
		
		private function onMouseLeaveFromStage(event:Event):void 
		{
			onStageMouseUp();
		};
		
		private function onIconMouseDown(event:MouseEvent = null):void 
		{
			//trace('ICON CLICK');
			_pressed = true; 
			var drag_rect:Rectangle = new Rectangle(0, 0, background.width, 0);
			icon.startDrag(false, drag_rect);
			
			// чтобы бы плавненько
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove);
			stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
			
			mute = false;
		};
		
		private function onStageMouseUp(event:MouseEvent = null):void 
		{
			icon.stopDrag();
			dispatchChange();
		};
		
		private function onStageMouseMove(event:MouseEvent):void 
		{
			position =  ( icon.x )  / (background.width);
			event.updateAfterEvent();
			dispatchChange();
		};
		
		private function onMouseDown(event:MouseEvent):void 
		{
			icon.x = mouseX;
			position =  ( icon.x )  / (background.width);
			onIconMouseDown();
			dispatchChange();
		};
		
		
		private function onMuteClick(event:MouseEvent):void 
		{
			_savedPosition = _position;
			position = 0;
			mute = true;
			dispatchChange();
		};
		
		private function onUnmuteClick(event:MouseEvent):void 
		{
			position = _savedPosition;
			mute = false;
			dispatchChange();
		};
		
	};

};